//
//  AutomaticPairVC.m
//  RTUApp
//
//  Created by 陈德军 on 2017/5/18.
//  Copyright © 2017年 陈德军. All rights reserved.
//

#import "AutomaticPairVC.h"
#import "KSDatePicker.h"

@interface AutomaticPairVC (){
    //使用当前系统时间
    UIButton *currentTimeBut;
    //对时次数限制
    UIButton *numberRestrictionsBut;
    //最终时间限制
    UIButton *lastTimeBut;
    //对时次数限制值
    UITextField *numberRestrictionsDetail;
    //对时参数底部线条
    UIView *bottomLineView1;
    //对时约束底部线条
    UIView *bottomLineView2;
    //开始日期
    UILabel *startdateDetail;
    NSDate *startdateDetailDate;
    //结束日期
    UILabel *overdateDetail;
    //开始时间
    UILabel *startTimeDetail;
    //结束时间
    UILabel *overTimeDetail;
    //发送间隔
    UITextField *sendIntervalF;
    //对时间隔
    UITextField *pairIntervalF;
    //显示日期
    UILabel *correctShowDate;
    //终端日期
    UILabel *terminalShowDate;
    //显示时间
    UILabel *correctShowTime;
    //终端时间
    UILabel *terminalShowTime;
    
    
    NSMutableString *str6;
    NSString *dataStr;
    int k;
    //判断开始对时是否被点击
    BOOL isClick;
}
//使用当前系统时间定时器
@property(strong, nonatomic) NSTimer *timer;
//开始对时定时器
@property(strong, nonatomic) NSTimer *startTimeTimer;
@end

@implementation AutomaticPairVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"蓝牙连接断开";
    self.view.backgroundColor = [UIColor whiteColor];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    startdateDetailDate = [dateFormatter dateFromString:[self getCurrentDate]];
    k=1;
    isClick = NO;
    UIScrollView *rootView = [UIScrollView new];
    self.view = rootView;
    rootView.userInteractionEnabled = YES;
    UITapGestureRecognizer *rootGTR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickRootView)];
    [rootView addGestureRecognizer:rootGTR];
    rootView.contentSize = CGSizeMake(kScreenWidth, 760);
    
    kHMBlueToothManager.failureBlock = ^(NSString *value) {
        
    };
    kHMBlueToothManager.bluetoothConnectionDisconnect = ^(NSString *value) {
        self.navigationController.navigationBar.barTintColor = KK_MailColor;
        self.navigationItem.title = @"蓝牙连接断开";
        [PopupView showMessageStr:@"蓝牙连接断开"];
    };
    [self addPairaPrametersView];
    [self addPairConstraintView];
    [self addMessageView];
    [self getDat];
    
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self getDat];
}

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"ConnectionStatus"] isEqualToString:@"连接成功"]) {
        self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0.48 green:0.69 blue:0.29 alpha:1.00];
        self.navigationItem.title = @"蓝牙连接成功";
    }else{
        self.navigationController.navigationBar.barTintColor = KK_MailColor;
        self.navigationItem.title = @"蓝牙连接断开";
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark 触发事件
-(void)getDat{
    NSData *data = [self stringToByte:@"6802000a017516"];
    [kHMBlueToothManager sendData:data];
    kHMBlueToothManager.dataBlock = ^(NSData *value) {
        NSLog(@"------------%@",value);
        NSMutableString *str1 = [NSMutableString stringWithFormat:@"%@",value];
        //第一个参数是要删除的字符的索引，第二个是从此位开始要删除的位数
        [str1 deleteCharactersInRange:NSMakeRange(0, 1)];
        [str1 deleteCharactersInRange:NSMakeRange(str1.length-1, 1)];
        //删除字符串中的空格
        NSString *str4 = [str1 stringByReplacingOccurrencesOfString:@" " withString:@""];
        NSString *str2 = [str4 substringToIndex:2];
        NSString *str3 = [str4 substringFromIndex:str4.length-2];
        if ([str2 isEqualToString:@"68"] && [str3 isEqualToString:@"16"]) {
            str6 = [NSMutableString stringWithFormat:@"%@",str4];
            if (![[str6 substringToIndex:6] isEqualToString:@"680800"] ) {
                return ;
            }
            dataStr = [NSMutableString stringWithFormat:@"%@",str4];
            [str6 deleteCharactersInRange:NSMakeRange(0, 10)];
            terminalShowDate.text = [NSString stringWithFormat:@"20%@-%@-%@",[self inFrontOfTheFill:[NSString stringWithFormat:@"%lu",strtoul([[str6 substringToIndex:2] UTF8String],0,16)]],[self inFrontOfTheFill:[NSString stringWithFormat:@"%lu",strtoul([[str6 substringWithRange:NSMakeRange(2, 2)] UTF8String],0,16)]],[self inFrontOfTheFill:[NSString stringWithFormat:@"%lu",strtoul([[str6 substringWithRange:NSMakeRange(4, 2)] UTF8String],0,16)]]];
            [str6 deleteCharactersInRange:NSMakeRange(0, 6)];
            terminalShowTime.text = [NSString stringWithFormat:@"%@:%@:%@",[self inFrontOfTheFill:[NSString stringWithFormat:@"%lu",strtoul([[str6 substringToIndex:2] UTF8String],0,16)]],[self inFrontOfTheFill:[NSString stringWithFormat:@"%lu",strtoul([[str6 substringWithRange:NSMakeRange(2, 2)] UTF8String],0,16)]],[self inFrontOfTheFill:[NSString stringWithFormat:@"%lu",strtoul([[str6 substringWithRange:NSMakeRange(4, 2)] UTF8String],0,16)]]];
        }
    };
}

- (void)clickStartDateEvent{
    //x,y 值无效，默认是居中的
    KSDatePicker* picker = [[KSDatePicker alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width - 40, 300)];
    //配置中心，详情见KSDatePikcerApperance
    picker.appearance.radius = 5;
    picker.datePickerMode = UIDatePickerModeDate;
    //设置回调
    picker.appearance.resultCallBack = ^void(KSDatePicker* datePicker,NSDate* currentDate,KSDatePickerButtonType buttonType){
        if (buttonType == KSDatePickerButtonCommit) {
            NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
            [formatter setDateFormat:@"yyyy-MM-dd"];
            startdateDetail.text = [formatter stringFromDate:currentDate];
            startdateDetailDate = currentDate;
        }
    };
    // 显示
    [self.view endEditing:YES];
    [picker show];
}

- (void)clickStartTimeEvent{
    KSDatePicker* picker = [[KSDatePicker alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width - 40, 300)];
    picker.datePickerMode = UIDatePickerModeTime;
    picker.appearance.radius = 5;
    picker.appearance.resultCallBack = ^void(KSDatePicker* datePicker,NSDate* currentDate,KSDatePickerButtonType buttonType){
        if (buttonType == KSDatePickerButtonCommit) {
            NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
            [formatter setDateFormat:@"HH:mm:00"];
            startTimeDetail.text = [formatter stringFromDate:currentDate];
        }
    };
    [self.view endEditing:YES];
    [picker show];
}

- (void)clickOverDateEvent{
    KSDatePicker* picker = [[KSDatePicker alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width - 40, 300)];
    picker.datePickerMode = UIDatePickerModeDate;
    picker.minimumDate = startdateDetailDate;
    picker.appearance.radius = 5;
    picker.appearance.resultCallBack = ^void(KSDatePicker* datePicker,NSDate* currentDate,KSDatePickerButtonType buttonType){
        if (buttonType == KSDatePickerButtonCommit) {
            NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
            [formatter setDateFormat:@"yyyy-MM-dd"];
            overdateDetail.text = [formatter stringFromDate:currentDate];
        }
    };
    [self.view endEditing:YES];
    [picker show];
}

- (void)clickOverTimeEvent{
    KSDatePicker* picker = [[KSDatePicker alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width - 40, 300)];
    picker.datePickerMode = UIDatePickerModeTime;
    picker.appearance.radius = 5;
    picker.appearance.resultCallBack = ^void(KSDatePicker* datePicker,NSDate* currentDate,KSDatePickerButtonType buttonType){
        if (buttonType == KSDatePickerButtonCommit) {
            NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
            [formatter setDateFormat:@"HH:mm:00"];
            overTimeDetail.text = [formatter stringFromDate:currentDate];
        }
    };
    [self.view endEditing:YES];
    [picker show];
}

- (void)clickStartCheckTimeButEvent{
    if (isClick) {
        return;
    }
    if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"ConnectionStatus"] isEqualToString:@"连接成功"]) {
        self.startTimeTimer = [NSTimer scheduledTimerWithTimeInterval:[sendIntervalF.text intValue]
                                                               target:self
                                                             selector:@selector(startTimeReduceTime)
                                                             userInfo:nil
                                                              repeats:YES];
    }else{
        [PopupView showMessageStr:@"请先连接蓝牙设备"];
        return;
    }
    
    isClick = YES;
    
}

- (void)clickStopCheckTimeButEvent{
    if (!isClick) {
        [PopupView showMessageStr:@"对时没有进行!"];
    }else{
        [self.startTimeTimer invalidate];
        isClick = NO;
    }
    
}

- (void)clickTerminalTimeButEvent{
    [self getDat];
}

- (void)clickCurrentTimeButEvent{
    if (currentTimeBut.selected) {
        currentTimeBut.selected=NO;
        [currentTimeBut setImage:[UIImage imageNamed:@"autoimageff"] forState:(UIControlStateNormal)];
        [self.timer invalidate];
    }else{
        currentTimeBut.selected=YES;
        [currentTimeBut setImage:[UIImage imageNamed:@"autotime"] forState:(UIControlStateNormal)];
        self.timer = [NSTimer scheduledTimerWithTimeInterval:1
                                                      target:self
                                                    selector:@selector(reduceTime)
                                                    userInfo:nil
                                                     repeats:YES];
    }
}


- (void)clickNumberRestrictionsButtEvent{
    if (numberRestrictionsBut.selected) {
        numberRestrictionsBut.selected=NO;
        [numberRestrictionsBut setImage:[UIImage imageNamed:@"autoimageff"] forState:(UIControlStateNormal)];
    }else{
        numberRestrictionsBut.selected=YES;
        [numberRestrictionsBut setImage:[UIImage imageNamed:@"autotime"] forState:(UIControlStateNormal)];
    }
    
}

- (void)clickLastTimeButEvent{
    if (lastTimeBut.selected) {
        lastTimeBut.selected=NO;
        [lastTimeBut setImage:[UIImage imageNamed:@"autoimageff"] forState:(UIControlStateNormal)];
    }else{
        lastTimeBut.selected=YES;
        [lastTimeBut setImage:[UIImage imageNamed:@"autotime"] forState:(UIControlStateNormal)];
    }
    
}

- (void)reduceTime{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"HH:mm:ss"];
    NSDate *datenow = [NSDate date];
    NSString *currentTimeString = [formatter stringFromDate:datenow];
    startTimeDetail.text = currentTimeString;
}

- (void)startTimeReduceTime{
    NSDateFormatter * dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *date = [dateFormatter dateFromString:[NSString stringWithFormat:@"%@ %@",startdateDetail.text,startTimeDetail.text]];
    NSTimeInterval  interval = [pairIntervalF.text intValue]*60*k;
    NSDate *newdate = [[NSDate date] initWithTimeInterval:interval sinceDate:date];
    
    if (numberRestrictionsBut.selected && k>[numberRestrictionsDetail.text intValue]) {
        return;
    }
    NSDate *stopDate = [dateFormatter dateFromString:[NSString stringWithFormat:@"%@ %@",overdateDetail.text,overTimeDetail.text]];
    NSDate *earlier_date = [stopDate earlierDate:newdate];
    if (lastTimeBut.selected && [earlier_date isEqual:stopDate]) {
        return;
    }
    
    NSDateFormatter * dateFormatter1 = [[NSDateFormatter alloc]init];
    [dateFormatter1 setDateFormat:@"yyyy-MM-dd"];
    NSDateFormatter * dateFormatter2 = [[NSDateFormatter alloc]init];
    [dateFormatter2 setDateFormat:@"HH:mm:ss"];
    correctShowDate.text = [dateFormatter1 stringFromDate:newdate];
    correctShowTime.text = [dateFormatter2 stringFromDate:newdate];
    
    NSDateFormatter * dateFormatter3 = [[NSDateFormatter alloc]init];
    [dateFormatter3 setDateFormat:@"yy,MM,dd,HH,mm,ss"];

    NSMutableString *sendStr = [NSMutableString stringWithFormat:@"%@",[dataStr substringToIndex:22]];
    [sendStr replaceCharactersInRange:NSMakeRange(9, 1) withString:@"2"];
    NSArray *array = [[dateFormatter3 stringFromDate:newdate] componentsSeparatedByString:@","]; //字符串按照【分隔成数组
    NSMutableString *str1 = [NSMutableString new];  //替换字符串
    for (NSString *str in array) {
        NSString *newStr = [self inFrontOfTheFill:[[NSString alloc] initWithFormat:@"%1x",[str intValue]]];
        str1 = [NSMutableString stringWithFormat:@"%@",[str1 stringByAppendingString:newStr]];
    }
    [sendStr replaceCharactersInRange:NSMakeRange(10, 12) withString:str1];
    NSString *checksum = [self getAddtion:[self hexToByteArr:sendStr]];
    sendStr = [NSMutableString stringWithFormat:@"%@",[NSString stringWithFormat:@"%@%@16",sendStr,checksum]];
    dataStr = sendStr;
    NSData *data = [self stringToByte:dataStr];
    [kHMBlueToothManager sendMsgWithSubPackage:data];
    
    k++;
}

- (void)clickRootView{
    [self.view endEditing:YES];
}
#pragma mark 转换工具
//获取当地时间
- (NSString *)getCurrentDate {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    NSString *dateTime = [formatter stringFromDate:[NSDate date]];
    return dateTime;
}

//获取当前的时间
-(NSString*)getCurrentTime{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"HH:mm:00"];
    NSDate *datenow = [NSDate date];
    NSString *currentTimeString = [formatter stringFromDate:datenow];
    return currentTimeString;
}
-(NSData*)stringToByte:(NSString*)string
{
    NSString *hexString=[[string uppercaseString] stringByReplacingOccurrencesOfString:@" " withString:@""];
    NSLog(@"%lu",(unsigned long)[hexString length]);
    if ([hexString length]%2!=0) {
        return nil;
    }
    Byte tempbyt[1]={0};
    NSMutableData* bytes=[NSMutableData data];
    for(int i=0;i<[hexString length];i++)
    {
        unichar hex_char1 = [hexString characterAtIndex:i]; ////两位16进制数中的第一位(高位*16)
        int int_ch1;
        if(hex_char1 >= '0' && hex_char1 <='9')
            int_ch1 = (hex_char1-48)*16;   //// 0 的Ascll - 48
        else if(hex_char1 >= 'A' && hex_char1 <='F')
            int_ch1 = (hex_char1-55)*16; //// A 的Ascll - 65
        else
            return nil;
        i++;
        
        unichar hex_char2 = [hexString characterAtIndex:i]; ///两位16进制数中的第二位(低位)
        int int_ch2;
        if(hex_char2 >= '0' && hex_char2 <='9')
            int_ch2 = (hex_char2-48); //// 0 的Ascll - 48
        else if(hex_char2 >= 'A' && hex_char2 <='F')
            int_ch2 = hex_char2-55; //// A 的Ascll - 65
        else
            return nil;
        
        tempbyt[0] = int_ch1+int_ch2;  ///将转化后的数放入Byte数组里
        [bytes appendBytes:tempbyt length:1];
    }
    return bytes;
}



-(NSString *)stringFromHexString:(NSString *)hexString { //
    
    char *myBuffer = (char *)malloc((int)[hexString length] / 2 + 1);
    bzero(myBuffer, [hexString length] / 2 + 1);
    for (int i = 0; i < [hexString length] - 1; i += 2) {
        unsigned int anInt;
        NSString * hexCharStr = [hexString substringWithRange:NSMakeRange(i, 2)];
        NSScanner * scanner = [[NSScanner alloc] initWithString:hexCharStr];
        [scanner scanHexInt:&anInt];
        myBuffer[i / 2] = (char)anInt;
    }
    NSString *unicodeString = [NSString stringWithCString:myBuffer encoding:4];
    //    NSLog(@"------字符串------%@",unicodeString);
    return unicodeString;
}
//把 byte数组转成 Int类型的数据
-(int) lBytesToInt:(NSString *)string
{
    NSString *str1 = [NSString stringWithFormat:@"%lu",strtoul([[string substringToIndex:2] UTF8String],0,16)];
    NSString *str2 = [NSString stringWithFormat:@"%lu",strtoul([[string substringWithRange:NSMakeRange(2, 2)] UTF8String],0,16)];
    NSString *str3 = [NSString stringWithFormat:@"%lu",strtoul([[string substringWithRange:NSMakeRange(4, 2)] UTF8String],0,16)];
    
    int  temp1 = [str1 intValue];
    int  temp2 = [str2 intValue];
    int  temp3 = [str3 intValue];
    Byte byte1[] = {temp1,temp2,temp3};
    
    int height = 0;
    NSData * testData =[NSData dataWithBytes:byte1 length:4];
    for (int i = 0; i < [testData length]; i++)
    {
        if (byte1[[testData length]-i] >= 0)
        {
            height = height + byte1[[testData length]-i];
        } else
        {
            height = height + 256 + byte1[[testData length]-i];
        }
        height = height * 256;
    }
    if (byte1[0] >= 0)
    {
        height = height + byte1[0];
    } else {
        height = height + 256 + byte1[0];
    }
    return height;
}


- (int)hextoint:(NSString *)string
{
    NSString *str1 = [NSString stringWithFormat:@"%lu",strtoul([[string substringToIndex:2] UTF8String],0,16)];
    NSString *str2 = [NSString stringWithFormat:@"%lu",strtoul([[string substringWithRange:NSMakeRange(2, 2)] UTF8String],0,16)];
    NSString *str3 = [NSString stringWithFormat:@"%lu",strtoul([[string substringWithRange:NSMakeRange(4, 2)] UTF8String],0,16)];
    NSString *str4 = [NSString stringWithFormat:@"%lu",strtoul([[string substringWithRange:NSMakeRange(6, 2)] UTF8String],0,16)];
    
    int  temp1 = [str1 intValue];
    int  temp2 = [str2 intValue];
    int  temp3 = [str3 intValue];
    int  temp4 = [str4 intValue];
    Byte dataVal[] = {temp1,temp2,temp3,temp4};
    int reVal = -1;
    
    
    reVal = (int)dataVal[3] << 24;
    
    reVal += (int)dataVal[2] << 16;
    
    reVal += (int)dataVal[1] << 8;
    
    reVal += (int)dataVal[0];
    return reVal;
}

//普通字符串转换为十六进制的。
- (NSString *)hexStringFromString:(NSString *)string{
    NSData *myD = [string dataUsingEncoding:NSUTF8StringEncoding];
    Byte *bytes = (Byte *)[myD bytes];
    //下面是Byte 转换为16进制。
    NSString *hexStr=@"";
    for(int i=0;i<[myD length];i++)
        
    {
        NSString *newHexStr = [NSString stringWithFormat:@"%x",bytes[i]&0xff];///16进制数
        
        if([newHexStr length]==1)
            
            hexStr = [NSString stringWithFormat:@"%@0%@",hexStr,newHexStr];
        
        else
            
            hexStr = [NSString stringWithFormat:@"%@%@",hexStr,newHexStr];
    }
    return hexStr;
}





//测试
- (NSMutableArray *)hexToByteArr:(NSString *)str{
    int hexlen = (int)str.length;
    NSMutableArray *array = [NSMutableArray new];
    Byte result[str.length/2];
    if ([self isOdd:hexlen] == 1) {
        ++hexlen;
        str = [NSString stringWithFormat:@"0%@",str];
    }else{
        
    }
    
    int j = 0;
    for (int i = 0; i<hexlen; i+=2) {
        NSString *bstr = [NSString stringWithFormat:@"%lu",strtoul([[str substringWithRange:NSMakeRange(i, 2)] UTF8String],0,16)];
        if ([bstr intValue] >128) {
            bstr = [NSString stringWithFormat:@"%d",[bstr intValue]-256];
        }else if ([bstr intValue]<-128){
            bstr = [NSString stringWithFormat:@"%d",256+[bstr intValue]];
        }
        [array addObject:bstr];
        j++;
    }
    //    NSLog(@"%@",array);
    return array;
}

- (NSString *)getAddtion:(NSMutableArray *)byte{
    int len = byte.count;
    int num = 0;
    for (int i = 0; i<len; i++) {
        num += [byte[i] intValue];
        NSLog(@"基数：%@",[NSString stringWithFormat:@"%d",[byte[i] intValue]]);
        if (num>128) {
            num = num-256;
        }else if (num<-128){
            num = 256+num;
        }
        NSLog(@"求和：%@",[NSString stringWithFormat:@"%d",num]);
    }
    
    if (num<10 && num>0) {
        NSLog(@"--%@",[NSString stringWithFormat:@"0%@",[self ToHex:[[NSString stringWithFormat:@"0%d",num] intValue]]]);
        return [NSString stringWithFormat:@"0%@",[self ToHex:[[NSString stringWithFormat:@"0%d",num] intValue]]];
    }else if (num<0){
        num = 256+num;
        NSLog(@"==%@",[self ToHex:[[NSString stringWithFormat:@"%d",num] intValue]]);
        return [self ToHex:[[NSString stringWithFormat:@"%d",num] intValue]];
    }else{
        NSLog(@"++%@",[self ToHex:[[NSString stringWithFormat:@"%d",num] intValue]]);
        return [self ToHex:[[NSString stringWithFormat:@"%d",num] intValue]];
    }
}

- (int)isOdd:(int )num{
    return num & 1;
}


-(NSString *)ToHex:(long long int)tmpid
{
    NSString *nLetterValue;
    NSString *str =@"";
    long long int ttmpig;
    for (int i = 0; i<9; i++) {
        ttmpig=tmpid%16;
        tmpid=tmpid/16;
        switch (ttmpig)
        {
            case 10:
                nLetterValue =@"A";break;
            case 11:
                nLetterValue =@"B";break;
            case 12:
                nLetterValue =@"C";break;
            case 13:
                nLetterValue =@"D";break;
            case 14:
                nLetterValue =@"E";break;
            case 15:
                nLetterValue =@"F";break;
            default:nLetterValue=[[NSString alloc]initWithFormat:@"%lli",ttmpig];
        }
        str = [nLetterValue stringByAppendingString:str];
        if (tmpid == 0) {
            break;
        }
    }
    return str;
}

//判断数字是否小于10 如果小于前面补0
- (NSString *)inFrontOfTheFill:(NSString *)str{
    if (str.length == 1) {
        str = [NSString stringWithFormat:@"0%@",str];
    }
    return str;
}
#pragma mark 视图布局

//对时参数视图
- (void)addPairaPrametersView{
    UILabel *pairaPrameLabel = [UILabel new];
    pairaPrameLabel.text = @"对时参数";
    pairaPrameLabel.font = [UIFont systemFontOfSize:16];
    pairaPrameLabel.textColor = KK_MailColor;
    pairaPrameLabel.top = 10;
    pairaPrameLabel.left = 15;
    [pairaPrameLabel sizeToFit];
    [self.view addSubview:pairaPrameLabel];
    
    UIView *baseView = [UIView new];
    baseView.size = CGSizeMake(kScreenWidth-30, 170);
    baseView.left = 15;
    baseView.top = pairaPrameLabel.bottom+10;
    baseView.layer.borderWidth = 0.5;
    baseView.layer.borderColor = [UIColor grayColor].CGColor;
    [self.view addSubview:baseView];
    
    UILabel *startdateLabel = [UILabel new];
    startdateLabel.text = @"起始日期 :";
    startdateLabel.top = 15;
    startdateLabel.left = 5;
    [startdateLabel sizeToFit];
    [baseView addSubview:startdateLabel];
    startdateDetail = [UILabel new];
    startdateDetail.text = [self getCurrentDate];
    startdateDetail.textAlignment = NSTextAlignmentCenter;
    startdateDetail.size = CGSizeMake(150, 28);
    startdateDetail.left = startdateLabel.right + 20;
    startdateDetail.centerY = startdateLabel.centerY;
    startdateDetail.layer.borderWidth = 0.5;
    startdateDetail.layer.borderColor = [UIColor grayColor].CGColor;
    startdateDetail.userInteractionEnabled = YES;
    UITapGestureRecognizer *startDateGTR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickStartDateEvent)];
    [startdateDetail addGestureRecognizer:startDateGTR];
    [baseView addSubview:startdateDetail];
    
    UILabel *startTimeLabel = [UILabel new];
    startTimeLabel.text = @"起始时间 :";
    startTimeLabel.top = startdateLabel.bottom + 20;
    startTimeLabel.left = startdateLabel.left;
    [startTimeLabel sizeToFit];
    [baseView addSubview:startTimeLabel];
    startTimeDetail = [UILabel new];
    startTimeDetail.text = [self getCurrentTime];
    startTimeDetail.textAlignment = NSTextAlignmentCenter;
    startTimeDetail.size = CGSizeMake(150, 28);
    startTimeDetail.left = startTimeLabel.right + 20;
    startTimeDetail.centerY = startTimeLabel.centerY;
    startTimeDetail.layer.borderWidth = 0.5;
    startTimeDetail.layer.borderColor = [UIColor grayColor].CGColor;
    startTimeDetail.userInteractionEnabled = YES;
    UITapGestureRecognizer *startTimeGTR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickStartTimeEvent)];
    [startTimeDetail addGestureRecognizer:startTimeGTR];
    [baseView addSubview:startTimeDetail];
    
    UILabel *pairIntervalLabel = [UILabel new];
    pairIntervalLabel.text = @"对时间隔 :";
    pairIntervalLabel.top = startTimeLabel.bottom + 20;
    pairIntervalLabel.left = startdateLabel.left;
    [pairIntervalLabel sizeToFit];
    [baseView addSubview:pairIntervalLabel];
    pairIntervalF = [UITextField new];
    pairIntervalF.text = @"4";
    pairIntervalF.keyboardType = UIKeyboardTypeNumberPad;
    pairIntervalF.textAlignment = NSTextAlignmentCenter;
    pairIntervalF.size = CGSizeMake(150, 28);
    pairIntervalF.left = pairIntervalLabel.right + 20;
    pairIntervalF.centerY = pairIntervalLabel.centerY;
    pairIntervalF.layer.borderWidth = 0.5;
    pairIntervalF.layer.borderColor = [UIColor grayColor].CGColor;
    [baseView addSubview:pairIntervalF];
    UILabel *pairIntervalUnitLabel = [UILabel new];
    pairIntervalUnitLabel.text = @"分";
    pairIntervalUnitLabel.font = [UIFont systemFontOfSize:17];
    [pairIntervalUnitLabel sizeToFit];
    pairIntervalUnitLabel.centerY = pairIntervalF.centerY;
    pairIntervalUnitLabel.left = pairIntervalF.right + 20;
    [baseView addSubview:pairIntervalUnitLabel];
    
    
    UILabel *sendIntervalLabel = [UILabel new];
    sendIntervalLabel.text = @"发送间隔 :";
    sendIntervalLabel.top = pairIntervalLabel.bottom + 20;
    sendIntervalLabel.left = startdateLabel.left;
    [sendIntervalLabel sizeToFit];
    [baseView addSubview:sendIntervalLabel];
    sendIntervalF = [UITextField new];
    sendIntervalF.keyboardType = UIKeyboardTypeNumberPad;
    sendIntervalF.text = @"1";
    sendIntervalF.textAlignment = NSTextAlignmentCenter;
    sendIntervalF.size = CGSizeMake(150, 28);
    sendIntervalF.left = sendIntervalLabel.right + 20;
    sendIntervalF.centerY = sendIntervalLabel.centerY;
    sendIntervalF.layer.borderWidth = 0.5;
    sendIntervalF.layer.borderColor = [UIColor grayColor].CGColor;
    [baseView addSubview:sendIntervalF];
    UILabel *sendIntervalUnitLabel = [UILabel new];
    sendIntervalUnitLabel.text = @"秒";
    sendIntervalUnitLabel.font = [UIFont systemFontOfSize:17];
    [sendIntervalUnitLabel sizeToFit];
    sendIntervalUnitLabel.centerY = sendIntervalF.centerY;
    sendIntervalUnitLabel.left = sendIntervalF.right + 20;
    [baseView addSubview:sendIntervalUnitLabel];
    
    currentTimeBut = [UIButton new];
    currentTimeBut.size = CGSizeMake(20, 20);
    currentTimeBut.top = baseView.bottom + 10;
    currentTimeBut.left = baseView.left;
    [currentTimeBut setImage:[UIImage imageNamed:@"autoimageff"] forState:(UIControlStateNormal)];
    [currentTimeBut addTarget:self action:@selector(clickCurrentTimeButEvent) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:currentTimeBut];
    UILabel *currentTimeLabel = [UILabel new];
    currentTimeLabel.text = @"使用当前系统时间";
    [currentTimeLabel sizeToFit];
    currentTimeLabel.left = currentTimeBut.right + 10;
    currentTimeLabel.centerY = currentTimeBut.centerY;
    [self.view addSubview:currentTimeLabel];
    
    bottomLineView1 = [UIView new];
    bottomLineView1.size = CGSizeMake(kScreenWidth, 0.5);
    bottomLineView1.left = 0;
    bottomLineView1.top = currentTimeBut.bottom + 10;
    bottomLineView1.backgroundColor = [UIColor grayColor];
    [self.view addSubview:bottomLineView1];
}
//对时约束视图
- (void)addPairConstraintView{
    UILabel *pairaPrameLabel = [UILabel new];
    pairaPrameLabel.text = @"对时约束";
    pairaPrameLabel.font = [UIFont systemFontOfSize:16];
    pairaPrameLabel.textColor = KK_MailColor;
    pairaPrameLabel.top = bottomLineView1.bottom + 10;
    pairaPrameLabel.left = 15;
    [pairaPrameLabel sizeToFit];
    [self.view addSubview:pairaPrameLabel];
    
    UIView *baseView = [UIView new];
    baseView.size = CGSizeMake(kScreenWidth-30, 130);
    baseView.left = 15;
    baseView.top = pairaPrameLabel.bottom+10;
    baseView.layer.borderWidth = 0.5;
    baseView.layer.borderColor = [UIColor grayColor].CGColor;
    [self.view addSubview:baseView];
    
    UILabel *overdateLabel = [UILabel new];
    overdateLabel.text = @"结束日期 :";
    overdateLabel.top = 15;
    overdateLabel.left = 5;
    [overdateLabel sizeToFit];
    [baseView addSubview:overdateLabel];
    overdateDetail = [UILabel new];
    overdateDetail.text = [self getCurrentDate];
    overdateDetail.textAlignment = NSTextAlignmentCenter;
    overdateDetail.size = CGSizeMake(150, 28);
    overdateDetail.left = overdateLabel.right + 20;
    overdateDetail.centerY = overdateLabel.centerY;
    overdateDetail.layer.borderWidth = 0.5;
    overdateDetail.layer.borderColor = [UIColor grayColor].CGColor;
    overdateDetail.userInteractionEnabled = YES;
    UITapGestureRecognizer *overDateGTR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickOverDateEvent)];
    [overdateDetail addGestureRecognizer:overDateGTR];
    [baseView addSubview:overdateDetail];
    
    
    UILabel *overTimeLabel = [UILabel new];
    overTimeLabel.text = @"结束时间 :";
    overTimeLabel.top = overdateLabel.bottom + 20;
    overTimeLabel.left = overdateLabel.left;
    [overTimeLabel sizeToFit];
    [baseView addSubview:overTimeLabel];
    overTimeDetail = [UILabel new];
    overTimeDetail.text = [self getCurrentTime];
    overTimeDetail.textAlignment = NSTextAlignmentCenter;
    overTimeDetail.size = CGSizeMake(150, 28);
    overTimeDetail.left = overTimeLabel.right + 20;
    overTimeDetail.centerY = overTimeLabel.centerY;
    overTimeDetail.layer.borderWidth = 0.5;
    overTimeDetail.layer.borderColor = [UIColor grayColor].CGColor;
    overTimeDetail.userInteractionEnabled = YES;
    UITapGestureRecognizer *overTimeGTR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickOverTimeEvent)];
    [overTimeDetail addGestureRecognizer:overTimeGTR];
    [baseView addSubview:overTimeDetail];
    
    numberRestrictionsBut = [UIButton new];
    numberRestrictionsBut.size = CGSizeMake(20, 20);
    numberRestrictionsBut.top = overTimeLabel.bottom + 20;
    numberRestrictionsBut.left = overTimeLabel.left;
    [numberRestrictionsBut setImage:[UIImage imageNamed:@"autoimageff"] forState:(UIControlStateNormal)];
    [numberRestrictionsBut addTarget:self action:@selector(clickNumberRestrictionsButtEvent) forControlEvents:(UIControlEventTouchUpInside)];
    [baseView addSubview:numberRestrictionsBut];
    UILabel *numberRestrictionsLabel = [UILabel new];
    numberRestrictionsLabel.text = @"对时次数限制";
    [numberRestrictionsLabel sizeToFit];
    numberRestrictionsLabel.left = numberRestrictionsBut.right + 10;
    numberRestrictionsLabel.centerY = numberRestrictionsBut.centerY;
    [baseView addSubview:numberRestrictionsLabel];
    numberRestrictionsDetail = [UITextField new];
    numberRestrictionsDetail.keyboardType = UIKeyboardTypeNumberPad;
    numberRestrictionsDetail.text = @"1";
    numberRestrictionsDetail.textAlignment = NSTextAlignmentCenter;
    numberRestrictionsDetail.size = CGSizeMake(overTimeLabel.size.width+overTimeDetail.size.width-10-numberRestrictionsLabel.size.width-10, overTimeDetail.size.height);
    numberRestrictionsDetail.left = numberRestrictionsLabel.right + 10;
    numberRestrictionsDetail.centerY = numberRestrictionsLabel.centerY;
    numberRestrictionsDetail.layer.borderWidth = 0.5;
    numberRestrictionsDetail.layer.borderColor = [UIColor grayColor].CGColor;
    [baseView addSubview:numberRestrictionsDetail];
    
    lastTimeBut = [UIButton new];
    lastTimeBut.size = CGSizeMake(20, 20);
    lastTimeBut.top = baseView.bottom + 10;
    lastTimeBut.left = baseView.left;
    [lastTimeBut setImage:[UIImage imageNamed:@"autoimageff"] forState:(UIControlStateNormal)];
    [lastTimeBut addTarget:self action:@selector(clickLastTimeButEvent) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:lastTimeBut];
    UILabel *lastTimeLabel = [UILabel new];
    lastTimeLabel.text = @"最终时间限制";
    [lastTimeLabel sizeToFit];
    lastTimeLabel.left = lastTimeBut.right + 10;
    lastTimeLabel.centerY = lastTimeBut.centerY;
    [self.view addSubview:lastTimeLabel];
    
    bottomLineView2 = [UIView new];
    bottomLineView2.size = CGSizeMake(kScreenWidth, 0.5);
    bottomLineView2.left = 0;
    bottomLineView2.top = lastTimeBut.bottom + 10;
    bottomLineView2.backgroundColor = [UIColor grayColor];
    [self.view addSubview:bottomLineView2];
    
}
//信息视图
- (void)addMessageView{
    UILabel *MessageLabel = [UILabel new];
    MessageLabel.text = @"信息";
    MessageLabel.font = [UIFont systemFontOfSize:16];
    MessageLabel.textColor = KK_MailColor;
    MessageLabel.top = bottomLineView2.bottom + 10;
    MessageLabel.left = 15;
    [MessageLabel sizeToFit];
    [self.view addSubview:MessageLabel];
    
    UIView *baseView = [UIView new];
    baseView.size = CGSizeMake(kScreenWidth-30, 70);
    baseView.left = 15;
    baseView.top = MessageLabel.bottom+10;
    baseView.layer.borderWidth = 0.5;
    baseView.layer.borderColor = [UIColor grayColor].CGColor;
    [self.view addSubview:baseView];
    
    UILabel *correctTime = [UILabel new];
    correctTime.text = @"校对时间:";
    correctTime.top = 10;
    correctTime.left = 20;
    correctTime.size = CGSizeMake(120, 20);
    [baseView addSubview:correctTime];
    correctShowDate = [UILabel new];
    correctShowDate.text = @"显示日期";
    correctShowDate.top = correctTime.top;
    correctShowDate.left = correctTime.right;
    correctShowDate.size = CGSizeMake(120, 20);
    [baseView addSubview:correctShowDate];
    correctShowTime = [UILabel new];
    correctShowTime.text = @"显示时间";
    correctShowTime.top = correctShowDate.top;
    correctShowTime.left = correctShowDate.right;
    correctShowTime.size = CGSizeMake(120, 20);
    [baseView addSubview:correctShowTime];
    
    UILabel *terminalTime = [UILabel new];
    terminalTime.text = @"终端时间:";
    terminalTime.top = correctTime.bottom + 10;
    terminalTime.left = correctTime.left;
    terminalTime.size = CGSizeMake(120, 20);
    [baseView addSubview:terminalTime];
    terminalShowDate = [UILabel new];
    terminalShowDate.text = @"显示日期";
    terminalShowDate.top = terminalTime.top;
    terminalShowDate.left = terminalTime.right;
    terminalShowDate.size = CGSizeMake(120, 20);
    [baseView addSubview:terminalShowDate];
    terminalShowTime = [UILabel new];
    terminalShowTime.text = @"显示时间";
    terminalShowTime.top = terminalShowDate.top;
    terminalShowTime.left = terminalShowDate.right;
    terminalShowTime.size = CGSizeMake(120, 20);
    [baseView addSubview:terminalShowTime];
    
    UIButton *startCheckTimeBut = [UIButton new];
    [startCheckTimeBut setTitle:@"开始对时" forState:(UIControlStateNormal)];
    startCheckTimeBut.size = CGSizeMake(100, 33);
    startCheckTimeBut.top = baseView.bottom +10;
    startCheckTimeBut.left = (kScreenWidth-300)/4;
    startCheckTimeBut.layer.cornerRadius = 6;
    startCheckTimeBut.backgroundColor = KK_MailColor;
    [startCheckTimeBut addTarget:self action:@selector(clickStartCheckTimeButEvent) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:startCheckTimeBut];
    
    UIButton *stopCheckTimeBut = [UIButton new];
    [stopCheckTimeBut setTitle:@"停止对时" forState:(UIControlStateNormal)];
    stopCheckTimeBut.size = CGSizeMake(100, 33);
    stopCheckTimeBut.top = startCheckTimeBut.top;
    stopCheckTimeBut.left = startCheckTimeBut.right +(kScreenWidth-300)/4;
    stopCheckTimeBut.layer.cornerRadius = 6;
    stopCheckTimeBut.backgroundColor = KK_MailColor;
    [stopCheckTimeBut addTarget:self action:@selector(clickStopCheckTimeButEvent) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:stopCheckTimeBut];
    
    UIButton *terminalTimeBut = [UIButton new];
    [terminalTimeBut setTitle:@"终端时间" forState:(UIControlStateNormal)];
    terminalTimeBut.size = CGSizeMake(100, 33);
    terminalTimeBut.top = startCheckTimeBut.top;
    terminalTimeBut.left = stopCheckTimeBut.right + (kScreenWidth-300)/4;
    terminalTimeBut.layer.cornerRadius = 6;
    terminalTimeBut.backgroundColor = KK_MailColor;
    [terminalTimeBut addTarget:self action:@selector(clickTerminalTimeButEvent) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:terminalTimeBut];
}
@end
